<?php

/**
 * alltosun.com   总后台角色管理   index.php
 * ============================================================================
 * 版权所有 (C) 2009-2014 北京互动阳光科技有限公司，并保留所有权利。
 * 网站地址: http://www.alltosun.com
 * ----------------------------------------------------------------------------
 * 许可声明: 这是一个开源程序，未经许可不得将本软件的整体或任何部分用于商业用途及再发布。
 * ============================================================================
 * $Author: 吴松键 (wusj@alltosun.com) $
 * $Date: 2014-8-25 上午12:08:33 $
 * $Id$
 */

class Action
{
    private $per_page = 20;
    /**
     * 显示角色列表
     */

    public function index()
    {
        $page          = Request::Get('page_no', 1);
        $status        = Request::Get('status', 1);

        // 拼装搜索条件
        $filter = array();
        $filter['status'] = $status;

        $role_list = array();
        $role_count = _model('role')->getTotal($filter);
        if ($role_count) {
            $pager = new Pager($this->per_page);
            if ($pager->generate($role_count)) {
                Response::assign('pager', $pager);
            }

            $role_list = _model('role')->getList($filter, ' ORDER BY `id` DESC '.$pager->getLimit($page));
        }

        Response::assign('role_list', $role_list);
        Response::assign('status', $status);

        Response::display('admin/role_list.html');
    }

    /**
     * 添加角色
     */
    public function add()
    {
        $filter['parent_id'] = 0;
        $filter['status']    = 1;

        $admin_menu_list = array();
        // 获取顶级菜单
        $admin_top_menu_list = _model('admin_menu')->getList($filter, 'ORDER BY `view_order`');
        foreach ($admin_top_menu_list as $k=>$v) {
            $admin_menu_list[$k] = $v;
            // 获取次级菜单
            $filter['parent_id'] = $v['id'];
            $children = _model('admin_menu')->getList($filter, 'ORDER BY `view_order`');
            if ($children) {
                $admin_menu_list[$k]['children'] = $children;
            }
        }

        $role_list = _model('role')->getList(array('`id` >'=>0));

        Response::assign('role_list', $role_list);
        Response::assign('admin_menu_list', $admin_menu_list);

        Response::display('admin/add.html');
    }

    /**
     * 编辑角色
     */
    public function edit()
    {
        $id = Request::getParam('id', 0);

        $filter['parent_id'] = 0;
        $filter['status']    = 1;

        $admin_menu_list = array();
        // 获取顶级菜单
        $admin_top_menu_list = _model('admin_menu')->getList($filter, 'ORDER BY `view_order`');
        foreach ($admin_top_menu_list as $k=>$v) {
            $admin_menu_list[$k] = $v;
            // 获取次级菜单
            $filter['parent_id'] = $v['id'];
            $children = _model('admin_menu')->getList($filter, 'ORDER BY `view_order`');
            if ($children) {
                $admin_menu_list[$k]['children'] = $children;
            }
        }

        if ($id) {
            $role_info = _uri('role', $id);

            Response::assign('role_info', $role_info);
            Response::assign('id', $id);
        }

        $role_list = _model('role')->getList(array('`id` >'=>0));

        Response::assign('role_list', $role_list);
        Response::assign('role_list', $role_list);
        Response::assign('admin_menu_list', $admin_menu_list);

        Response::display('admin/add.html');
    }

    /**
     * 保存
     */
    public function save()
    {
        $info = Request::getParam('info', array());
        $id   = Request::getParam('id', 0);

        foreach ($info as $k=>$v) {
            if (!is_numeric($v)) {
                if (is_array($v)) {
                    foreach ($v as $key=>$value) {
                        $info[$k][$key] = trim(AnFilter::filter_string($value));
                    }
                } else {
                    $info[$k] = trim(AnFilter::filter_string($v));
                }
            }
        }

        if (!isset($info['title']) || !$info['title']) {
            return '请填写角色标题';
        }

        if (!isset($info['type']) || !$info['type']) {
            return '请选择角色类型';
        }

        $info['privilege'] = join(',', $info['privilege']);
        if ($id) {
            $role_info = _uri('role', $id);
            if (!$role_info) {
                return '该内容不存在';
            }

            _model('role')->update($id, $info);
        } else {
            $role_id = _model('role')->create($info);
        }

        Response::redirect(AnUrl('role/admin'));
    }

    /**
     * 删除
     * @return array
     */
    public function delete()
    {
        $id = Request::getParam('id');

        if (!$id) {
            return '请选择要删除的内容';
        }

        $ids = explode(',', trim($id, ','));
        foreach ($ids as $k=>$v) {
            if (!$v) {
                continue;
            }

            $role_info = _uri('role', (int)$v);
            if (!$role_info) {
                continue;
            }

            if (1 == $role_info['status']) {
                _model('role')->update($v, array('status'=>0));
            } else {
                _model('role')->delete($v);
            }
        }

        return 'ok';
    }

    /**
     * 显示成员列表
     */
    public function user_manage()
    {
        $id            = Request::getParam('id', 0);
        $page          = Request::Get('page_no', 1);
        $search_filter = Request::getParam('search_filter', array());

        // 拼装搜索条件
        $filter = array();
        if (isset($search_filter['user_name']) && $search_filter['user_name']) {
            $user_ids = user_helper::get_user_ids_by_name($search_filter['user_name']);
            if ($user_ids) {
                $filter['user_id'] = $user_ids;
            }
        }
        $filter['role_id'] = $id;
        $role_info = array();
        $role_user_list = array();
        if ($id) {
            $role_info = _uri('role', $id);

            $role_user_count = _model('role_user')->getTotal($filter);
            if ($role_user_count) {
                $pager = new Pager($this->per_page);
                if ($pager->generate($role_user_count)) {
                    Response::assign('pager', $pager);
                }

                $role_user_list = _model('role_user')->getList($filter, ' ORDER BY `user_id` DESC '.$pager->getLimit($page));
            }
        }

        Response::assign('role_info', $role_info);
        Response::assign('role_user_list', $role_user_list);
        Response::assign('search_filter', $search_filter);
        Response::assign('id', $id);

        Response::display('admin/user_list.html');
    }

    /**
     * 添加角色
     */
    public function add_user()
    {
        $id = Request::getParam('id', 0);

        $role_list = _model('role')->getList(array('`id` >'=>0));

        Response::assign('role_list', $role_list);
        Response::assign('id', $id);

        Response::display('admin/add_user.html');
    }

    /**
     * 保存成员
     */
    public function save_user()
    {
        $info = Request::getParam('info', array());
        $id   = Request::getParam('id', 0);

        if (!isset($info['role_id']) || !$info['role_id']) {
            return '请选择角色组名称';
        }

        if (!isset($info['user_id']) || !$info['user_id']) {
            return '请填写正确的用户名';
        }

        $role_user_info = _uri('role_user', $info);
        if (!empty($role_user_info['id'])) {
            _model('role_user')->update(array('user_id' => $info['user_id']) ,array('role_id' => $info['role_id']));
            return '用户角色调整成功';
        } else {
            _model('role_user')->create($info);
        }

        Response::redirect(AnUrl('role/admin/user_manage?id='.$info['role_id']));
    }

    /**
     * 删除成员
     * @return array
     */
    public function delete_user()
    {
        $user_id = Request::Post('user_id', 0);
        $role_id = Request::Post('role_id', 0);
        if (!$user_id && !$role_id) {
            return '请选择要删除的内容';
        }

        $filter = array(
                'user_id' => $user_id,
                'role_id' => $role_id
        );

        $affected = _model('role_user')->delete($filter);
        if($affected) {
            return 'ok';
        } else {
            return '网络错误，删除失败！';
        }

    }

    /**
     * 更新状态
     * @return array
     */
    public function change_status()
    {
        $id     = Request::getParam('id', 0);
        $status = Request::getParam('status', 1);
        if (!$id) {
            return '请选择要更改的内容';
        }

        $role_info = _uri('role', $id);
        if ($status == $role_info['status']) {
            return '已经执行过此操作，不用重复提交';
        }

        if (1 == $status) {
            _model('role')->update($id, array('status'=>1));
        }

        return 'ok';
    }

    /**
     * ajax查找用户列表
     * @return array
     */
    public function find_user_list()
    {
        $user_name = Request::getParam('user_name', '');

        if ($user_name == '') {
            return '';
        }

        $search = "AND (`user_name` LIKE '%".AnFilter::filter_string($user_name)."%'";
        $search .= intval($user_name) ? " OR `mobile` LIKE '%".intval($user_name)."%')" : ')';
        $filter['status']= 1;

        $user_list = _model('user')->getList($filter, $search.' ORDER BY `id` ASC LIMIT 0,20');

        return array('info'=>'ok', 'user_list'=>$user_list);
    }
}
?>